import urls from "../../common/js/api_url_array";

export default {
  data() {
    return {
      //数据
      validate: {}, //验证对象
      template: {}, //模板对象
    }
  },
  created() { //调用方法

  },
  mounted() {
    let that = this;
    let uuid = this.get("templateUuid");
    if(uuid != null && uuid != "") {
      that.$post(
        that.urls.getTemplate, {
          "uuid": uuid
        }
      )
      .then(function(res) {
        if(res.success) {
          that.template = res.data;
          $("#urlLi").html(that.getFileName(that.template.url));
        }
      })
    }

    //校验名称重复
    $.validator.addMethod("checkNameRepeat", function(value, element, params) {
      let flg = false;
      if(that.template.id != null && (value == that.template.name)) {
        flg = true;
      } else {
        $.ajax({
          type: "POST",
          async: false,
          url: that.urls.checkTemplateName,
          data: JSON.stringify({
            "name": value
          }),
          dataType: "json",
          contentType: "application/json",
          success: function(data) {
            if(data.success) {
              flg = !data.data;
            }
          }
        });
      }
      return this.optional(element) || flg;
    }, "该标题已存在！");

    //校验类型重复
    $.validator.addMethod("checkTypeRepeat", function(value, element, params) {
      let flg = false;
      if(that.template.id != null && (value == that.template.type)) {
        flg = true;
      } else {
        $.ajax({
          type: "POST",
          async: false,
          url: that.urls.checkTemplateType,
          data: JSON.stringify({
            "type": value
          }),
          dataType: "json",
          contentType: "application/json",
          success: function(data) {
            if(data.success) {
              flg = !data.data;
            }
          }
        });
      }
      return this.optional(element) || flg;
    }, "该类型已存在！");

    //加载表单验证
    that.validate = $("#templateForm").validate({
      ignore: [],
      rules: {
        name: { // 政策发布名称
          required: true,
          maxlength: 50,
          checkNameRepeat: true
        },
        type: { // 类型
          required: true,
          checkTypeRepeat: true
        },
        url: { // 文件地址
          required: true,
          maxlength: 500
        }
      },
      messages: {
        name: { // 政策发布名称
          required: "请输入标题！",
          maxlength: "标题最多只能输入50个字！"
        },
        type: { // 类型
          required: "请选择类型！"
        },
        url: { // 文件地址
          required: "请选择文件！",
          maxlength: "文件地址超长！"
        }
      },
//			errorElement: "em",
//			errorPlacement: function(error, element) {
//				error.appendTo(element.parents(".error-box").children(".error-msg"));
//			}
    });
  },
  methods: { //写函数方法
    touchOffIdInput(n) {
      this.uploadNum = n;
      if(n == 1) {
        $("#handIdFront").click();
      }
    },
    //删除附件
    deleteEnclosure: function() {
      $("#urlLi").html("");
      $("input[name='url']").val("");
    },
    idFrontImg: function(event) {
      var that = this;
      var file = event.currentTarget.files[0];
      if (!that.fileGlobalFilter(file)) {
        return;
      }
      let formData = new FormData();
      formData.append('fileNames', file);
      let config = {
        headers: {
          'Content-Type': 'multipart/form-data;boundary = ' + new Date().getTime()
        }
      };
      that.$post(
        that.urls.upload,
        formData,
        config
      )
        .then(function(res) {
          if(res.success) {
            let fileInfo = res.data[0];
            $("input[name='url']").val(fileInfo.url);
            $("#urlLi").html(that.getFileName(fileInfo.url));
          }
        })
    },
    //保存
    saveData: function() {
      let that = this;
      let flg = that.validate.form();
      if(flg) {
        let dataObj = combinationFormObject(that.validate.currentForm);
        that.$post(
          that.urls.addTemplate,
          dataObj
        )
          .then(function(res) {
            let msg = "";
            if(res.success) {
              that.policyRelease = res.data;
              msg = "保存成功！";
              that.pressRelease();
            } else {
              msg = "保存失败！";
            }
            layer.msg(msg, {
              time: 3000, //3s后自动关闭
            });
          })
      }
    },
    //修改
    updateData: function() {
      let that = this;
      let flg = that.validate.form();
      if(flg) {
        let dataObj = combinationFormObject(that.validate.currentForm);
        that.$post(
          that.urls.updateTemplate,
          dataObj
        )
          .then(function(res) {
            let msg = "";
            if(res.success) {
              msg = "修改成功！";

            } else {
              msg = "修改失败！";
            }
            layer.msg(msg,function(){
              that.pressRelease();
            });
          })
      }
    },
    //返回模板列表页
    pressRelease: function() {
      var that = this;
      that.$router.push({
        path: "templateManagement",
      })
    },
  }
}
